package com.weipai.demo.tool;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.connection.HttpProxy;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class Jpush {

	protected static final Logger LOG = LoggerFactory.getLogger(Jpush.class);
	protected static final String APP_KEY = "fe96b92a2f4d9bc2813bad47";
	protected static final String MASTER_SECRET = "119d9fa0a4e38cf739230590";
	protected ClientConfig clientConfig = ClientConfig.getInstance();
	protected HttpProxy proxy = null;
	protected JPushClient jPushClient = null;

//	public Response push(String title,String message,Audience audience,Map<String, String> map){
//		Jpush jpush = new Jpush();
//		jpush.push("消息标题", "哈哈哈", "", Audience.all(), new HashMap<String, String>());
//	}

	public JPushClient getJPushClient(){
		//代理设置
		proxy = new HttpProxy("192.168.100.36", 8088);
		clientConfig = ClientConfig.getInstance();
		jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
//		jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
		return jPushClient;
	}

	public PushResult push(String title, String message, String extras,
						   Audience audience, Map<String, String> map) {
		PushResult result = null;
		try {
			result = getJPushClient().sendPushValidate(buildPushObject_android_and_ios(
					title, message,extras,audience,map));
			System.out.println(result);
			return result;
		} catch (APIConnectionException e) {
			LOG.error(e.getMessage(), e);
		} catch (APIRequestException e) {
			LOG.error(e.getMessage(), e);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
		return result;

	}

	/**
	 *
	 *  @Description:
	 *  @param title 标题
	 *  @param message 消息内容
	 *  @param extras 额外字段
	 *  @param audience	推送设备指定
	 *  @param map 自定义消息map集合
	 *  @return
	 */
	public PushPayload buildPushObject_android_and_ios(String title,
													   String message, String extras, Audience audience,
													   Map<String, String> map) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.all())
				.setNotification(Notification.alert("1")).build();
//						Notification
//								.newBuilder()
//								.addPlatformNotification(
//										AndroidNotification.newBuilder()
//												.setTitle(title)
//												.setAlert(message)
//												.addExtra("extras", extras)
//												.build()).build())
//				.setMessage(
//						Message.newBuilder().setMsgContent(message)
//								.setTitle(title).addExtras(map)
//								.build())
//				.setOptions(Options.newBuilder().setApnsProduction(false)// 仅仅对于IOS生效.false开发环境
//						.build())
//				.build();
	}

	/**
	 * 生成极光推送对象PushPayload（采用java SDK）
	 * @param alias
	 * @param alert
	 * @return PushPayload
	 */
	public static PushPayload buildPushObject_android_ios_alias_alert(String title,String message){
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(title))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(AndroidNotification.newBuilder()
								.addExtra("type", "infomation")
								.setAlert(message)
								.build())
						.build())
				.setOptions(Options.newBuilder()
						.setApnsProduction(false)//true-推送生产环境 false-推送开发环境（测试使用参数）
						.setTimeToLive(90)//消息在JPush服务器的失效时间（测试使用参数）
						.build())
				.build();
	}



}